package server

import (
	"encoding/json"
	"html/template"
	"net/http"
	"time"
)

// 默认访问的网页
func Default(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("template/default.html")
	t.Execute(w, nil)
}

// 表格
type Log struct {
	Name string
	Time   time.Time
}

// 登陆提交表单，并输出一个表格
func Login(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	if r.Method == "GET" {
		t, _ := template.ParseFiles("template/login.html")
		t.Execute(w, nil)
	} else {
		log := &Log{r.Form["username"][0], time.Now()}
		t, _ := template.ParseFiles("template/table.html")
		t.Execute(w, log)
	}
}

type testResponse struct {
	ID            int    `json:"id"`
	Content  string `json:"content"`
}

// 简单 js 访问
func Test(w http.ResponseWriter, r *http.Request) {
	// Success
	response := &testResponse{200, "OK"}
	message, _ := json.Marshal(response)
	w.Write(message)
}
